package top.lishuoboy.boot92.datetime.serializable.controller;

import cn.hutool.core.date.DateUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import top.lishuoboy.boot92.datetime.serializable.domain.DateTimeVo;

import java.time.*;
import java.util.Calendar;
import java.util.Date;

@Slf4j
@RestController
@RequestMapping("/DateTimeSerializable")
public class DatetimeSerializableController {

    @PostMapping("/jackson")
    DateTimeVo jackson(@RequestBody DateTimeVo dateTimeVoReq) {
        log.info("dateTimeVoReq=={}", dateTimeVoReq);

        ZonedDateTime zonedDateTime = ZonedDateTime.parse("1970-01-01T00:00:00.111222333Z");
        Instant instant = Instant.from(zonedDateTime);

        // Local系列不包含时区
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        LocalDate localDate = localDateTime.toLocalDate();
        LocalTime localTime = localDateTime.toLocalTime();

        Date date = Date.from(instant);
        Calendar calendar = DateUtil.calendar(date);

        DateTimeVo dateTimeVoRes = DateTimeVo.builder()
            // 1.旧版-日期时间毫秒-含时区
            .date(date)
            .calendar(calendar)

            // 2.1.新版-日期时间纳秒-含时区
            .zonedDateTime(zonedDateTime)
            .instant(instant)

            // 2.2.新版-本地日期时间（不含时区）
            .localDateTime(localDateTime)
            .localDate(localDate)
            .localTime(localTime)

            // 2.3.新版-时间段
            .period(Period.between(localDate, localDate.plusYears(1).plusMonths(1).plusDays(1)))
            .duration(Duration.between(localDateTime, localDateTime.plusYears(0).plusMonths(0).plusDays(1).plusHours(1).plusMinutes(1).plusSeconds(1).plusNanos(1)))

            // 2.4.新版-其它
            .yearMonth(YearMonth.of(localDateTime.getYear(), localDateTime.getMonth()))
            .monthDay(MonthDay.of(localDateTime.getMonthValue(), localDateTime.getDayOfMonth()))
            .year(Year.of(localDateTime.getYear()))
            .month(Month.of(localDateTime.getMonthValue()))

            .build();
        log.info("dateTimeVoRes=={}", dateTimeVoRes);
        return dateTimeVoRes;
    }

    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "query", name = "calendar", value = "旧版-日期时间毫秒-含时区"),
        @ApiImplicitParam(paramType = "query", name = "localTime", value = "新版-本地时间纳秒-不含时区")
    })
    @ApiOperationSupport(includeParameters = {"date", "calendar", "zonedDateTime", "instant", "localDateTime", "localDate", "localTime", "localDateTime2", "localDateTime3"})
    @PostMapping("/mvc/{localDateTime3}")
    Object mvc(@ModelAttribute DateTimeVo dateTimeVoReq,
               @RequestParam(required = false) LocalDateTime localDateTime2,
               @PathVariable(required = false) LocalDateTime localDateTime3) {
        log.info("dateTimeVoReq=={}", dateTimeVoReq);
        log.info("localDateTime2=={}", localDateTime2);
        log.info("localDateTime3=={}", localDateTime3);
        return LocalTime.now();
    }


}
